import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/view/examination/question/question_text_widget.dart';
import 'package:kxy_flutter_app/view/examination/widget/option_type.dart';
import 'package:kxy_flutter_app/view/examination/widget/option_widget.dart';
import '../../../model/exam_question.dart';

class SingleChoiceQuestionWidget extends StatelessWidget {
  final Question question;
  final Function(String) checkCallback;
  final bool withResult;

  const SingleChoiceQuestionWidget({Key key, this.question, this.checkCallback, this.withResult = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      children: [
        QuestionTextWidget(text: question.title),
        optionListWidget(context),
        if (withResult && question.confirmed) resultWidget(),
      ],
    );
  }

  Widget resultWidget() {
    return Container(
      margin: EdgeInsets.only(top: 20),
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(
        color: Color.fromRGBO(250, 250, 250, 1),
        border: Border.all(color: Color.fromRGBO(230, 230, 230, 1), width: 1),
        borderRadius: BorderRadius.circular(5),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '答案解析',
            style: TextStyle(fontSize: 16, color: Colors.black54),
          ),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text(
              '正确答案：${question.rightAnswer}，您的答案：${question.userAnswer}',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text(
              question.analysis,
              style: TextStyle(fontSize: 16, color: Colors.black54),
            ),
          ),
        ],
      ),
    );
  }

  Widget optionListWidget(BuildContext context) {
    return Container(
      child: Column(
        children: ListTile.divideTiles(
          tiles: question.options.map((opt) {
            return GestureDetector(
              child: OptionWidget(type: getType(opt), option: opt),
              onTap: () => checkCallback(opt.key),
            );
          }),
          context: context,
          color: Color.fromRGBO(240, 240, 240, 1),
        ).toList(),
      ),
    );
  }

  OptionType getType(AnswerOption opt) {
    if (withResult && question.confirmed) {
      if (opt.key == question.rightAnswer) {
        if (opt.key == question.userAnswer) {
          return OptionType.successAndChecked;
        }
        return OptionType.success;
      }

      if (opt.key == question.userAnswer) {
        return OptionType.error;
      }

      return OptionType.noraml;
    }

    if (opt.key == question.userAnswer) {
      return OptionType.primary;
    }

    return OptionType.noraml;
  }
}
